package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类id查询菜品及口味
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品及口味")
    public Result<List<DishVO>> getDishes(long categoryId){

        String key = "dish_" + categoryId;
        //存在redis中则直接返回
        List<DishVO> listRedis = (List<DishVO>)redisTemplate.opsForValue().get(key);
        if(listRedis!=null && listRedis.size() !=0){
            return Result.success(listRedis);
        }

        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE).build();
        List<DishVO> list=dishService.listWithFlavor(dish);
        //不存在则直接查询数据库

        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);
    }

}
